<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * Get id divided table name for 3rd party user
 *
 * Because user id related data is huge, the table is divided by using fmod(id, 10).
 *
 * @param	string	prefix of table name
 * @param	integer id
 * @return	string  table name regarding id
 */
if ( ! function_exists('get_pdtable'))
{
	function get_pdtable($table, $id)
	{
		return $table.'0';//$table.fmod($id, 10);
	}
}

/**
 * Get id divided table name
 *
 * Because user id related data is huge, the table is divided by using fmod(id, 100).
 *
 * @param	string	prefix of table name
 * @param	integer id
 * @return	string  table name regarding id
 */
if ( ! function_exists('get_dtable'))
{
	function get_dtable($table, $id)
	{
		return $table.'0';//$table.fmod($id, 100);
	}
}

/**
 * Get id divided table name
 *
 * Because user id related data is huge, the table is divided by using fmod(uid, 100).
 *
 * @param	string	prefix of table name
 * @param	integer uid
 * @return	string  table name regarding uid
 */
if ( ! function_exists('get_utable'))
{
	function get_utable($table, $uid)
	{
		return $table.'0';//$table.fmod($id, 100);
	}
}

/**
 * Get app related table
 *
 * Because user id related data is huge, the table is divided by using app id.
 *
 * @param	string	prefix of table name
 * @param	integer app id
 * @return	string  table name regarding id
 */
if ( ! function_exists('get_autable'))
{
	function get_autable($table, $appid)
	{
		return $table.$appid;
	}
}

/**
 * Get a new user id
 *
 * Because it might create a new user account simultaneously, it should call this method to generate a new user id.
 *
 * @param	void
 * @return	integer new user id
 */
if ( ! function_exists('get_new_userid'))
{
	function get_new_userid()
	{
		$CI->load->model('user_id_gen');
		
		return $CI->account_id_gen->get_new_id();
	}
}

/**
 * Get a current max user id
 *
 * Get the current maximum user id.
 *
 * @param	void
 * @return	integer user id
 */
if ( ! function_exists('get_max_userid'))
{
	function get_max_userid()
	{
		$CI->load->model('user_id_gen');

		return $CI->account_id_gen->get_max_id();
	}
}

/**
 * Get value string for sql
 *
 * @param	array	key value array
 * @return	string	value string
 */
if ( ! function_exists('get_val_str'))
{
	function get_val_str($array)
	{
		$keys = array_keys($array);
		$str = ' ';
		for($i=0; $i<count($keys); ++$i)
		{
			if($i == 0)
				$str = $str.' (';
			else
				$str = $str.', ';
			
			$str = $str."`$keys[$i]`";
		}
		
		$str = $str.") VALUES ";
		
		for($i=0; $i<count($keys); ++$i)
		{
			if($i == 0)
				$str = $str.' (';
			else
				$str = $str.', ';
			
			$key = $keys[$i];
			$str = $str."'$array[$key]'";
		}
		
		$str = $str.")";
		
		return $str;
	}
}

/**
 * Create INSERT sql
 *
 * @param	table	table name
 * @param	data	key value array
 * @return	string	sql string
 */
if ( ! function_exists('create_insert_sql'))
{
	function create_insert_sql($table, $data)
	{
		if(empty($table) || empty($data))
			return null;

		$sql = 'INSERT INTO '.$table;
		$sql = $sql.get_val_str($data);

// 	    log_message('error', 'sql = '.$sql);
		return $sql;
	}
}

/**
 * Get value string for sql
 *
 * @param	array	key value array
 * @return	string	set string
 */
if ( ! function_exists('get_set_str'))
{
	function get_set_str($array)
	{
		$keys = array_keys($array);
		$str = ' SET ';
		for($i=0; $i<count($keys); ++$i)
		{
			$key = $keys[$i];
			
			if($i != 0)
				$str = $str.', ';
			
			$str = $str." $keys[$i] = '$array[$key]'";
		}

		return $str.' ';
	}
}

/**
 * Get where string for sql
 *
 * @param	cond	where condition array
 * @return	string	where string
 */
if ( ! function_exists('get_where_str'))
{
	function get_where_str($cond=null)
	{
		if(empty($cond))
			return null;
		
		$keys = array_keys($cond);
		$str = ' WHERE ';
		for($i=0; $i<count($keys); ++$i)
		{
			$key = $keys[$i];
				
			if($i != 0)
				$str = $str.' AND ';
				
			$str = $str." $keys[$i] = '$cond[$key]'";
		}

		return $str.' ';
	}
}

/**
 * Create UPDATE sql
 *
 * @param	table	table name
 * @param	data	new key value
 * @param	cond	where condition array
 * @return	string	sql string
 */
if ( ! function_exists('create_update_sql'))
{
	function create_update_sql($table, $data, $cond=null)
	{
		if(empty($table) || empty($data))
			return null;
		
		$sql = 'UPDATE '.$table;
		$sql = $sql.get_set_str($data);
		
		if(!empty($cond))
			$sql = $sql.get_where_str($cond);
		
// 	    log_message('error', 'sql = '.$sql);
		return $sql;
	}
}

/**
 * Create UPDATE sql
 *
 * @param	table	table name
 * @param	data	new key value
 * @param	cond	where condition array
 * @return	string	sql string
 */
if ( ! function_exists('create_delete_sql'))
{
	function create_delete_sql($table, $cond=null)
	{
		if(empty($table))
			return null;

		$sql = 'DELETE FROM '.$table;

		if(!empty($cond))
			$sql = $sql.get_where_str($cond);

// 	    log_message('error', 'sql = '.$sql);
		return $sql;
	}
}


